package com.bobgao.my_first_demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bobgao.my_first_demo.entity.DoHomeworkEntity;
import com.bobgao.my_first_demo.entity.HomeWorkEntity;
import com.bobgao.my_first_demo.mapper.DoHomeWorkMapper;
import com.bobgao.my_first_demo.mapper.HomeWorkMapper;
import com.bobgao.my_first_demo.req.DoHomeWorkReq;
import com.bobgao.my_first_demo.req.DoHomeWorkSaveReq;
import com.bobgao.my_first_demo.req.HomeWorkReq;
import com.bobgao.my_first_demo.req.HomeWorkSaveReq;
import com.bobgao.my_first_demo.resp.PageResp;
import com.bobgao.my_first_demo.service.HomeWorkService;
import com.bobgao.my_first_demo.utils.CopyUtil;
import com.bobgao.my_first_demo.utils.SnowFlake;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;

@Service
public class HomeWorkServiceImpl extends ServiceImpl<HomeWorkMapper, HomeWorkEntity> implements HomeWorkService {

    @Resource
    HomeWorkMapper homeWorkMapper;

    @Resource
    DoHomeWorkMapper doHomeWorkMapper;

    @Resource
    SnowFlake snowFlake;

    @Override
    public PageResp<HomeWorkEntity> getHomeworkList(HomeWorkReq homeWorkReq){

        QueryWrapper<HomeWorkEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(homeWorkReq.getWorkName())){
            queryWrapper.lambda().eq(HomeWorkEntity::getWorkName,homeWorkReq.getWorkName());
        }

        if (!ObjectUtils.isEmpty(homeWorkReq.getWorkTeacher())){
            queryWrapper.lambda().eq(HomeWorkEntity::getWorkTeacher,homeWorkReq.getWorkTeacher());
        }

        Page<HomeWorkEntity> page = new Page<>(homeWorkReq.getPage(),homeWorkReq.getSize());
        IPage<HomeWorkEntity> homeWorkEntityIPage = homeWorkMapper.selectPage(page,queryWrapper);
        PageResp<HomeWorkEntity> pageResp = new PageResp<>();
        pageResp.setTotal(homeWorkEntityIPage.getTotal());
        pageResp.setList(homeWorkEntityIPage.getRecords());
        return pageResp;
    }

    @Override
    public PageResp<DoHomeworkEntity> getDoHomeWorkList(DoHomeWorkReq doHomeWorkReq){

        QueryWrapper<DoHomeworkEntity> queryWrapper = new QueryWrapper<>();
        if (!ObjectUtils.isEmpty(doHomeWorkReq.getWorkNamedo())){
            queryWrapper.lambda().eq(DoHomeworkEntity::getWorkNamedo,doHomeWorkReq.getWorkNamedo());
        }

        Page<DoHomeworkEntity> page = new Page<>(doHomeWorkReq.getPage(),doHomeWorkReq.getSize());
        IPage<DoHomeworkEntity> doHomeworkEntityIPage = doHomeWorkMapper.selectPage(page,queryWrapper);
        PageResp<DoHomeworkEntity> pageResp = new PageResp<>();
        pageResp.setTotal(doHomeworkEntityIPage.getTotal());
        pageResp.setList(doHomeworkEntityIPage.getRecords());
        return pageResp;
    }

    @Override
    public void deleteHomework(Long id) {
        homeWorkMapper.deleteById(id);
    }

    @Override
    public void saveHomework(HomeWorkSaveReq req){
        HomeWorkEntity entity = CopyUtil.copy(req, HomeWorkEntity.class);
        if(ObjectUtils.isEmpty(req.getId())){
            entity.setId(snowFlake.nextId());
            homeWorkMapper.insert(entity);
        }else {
            System.out.println(entity);
            homeWorkMapper.updateById(entity);
        }
    }

    @Override
    public void DoHomeWork(DoHomeWorkSaveReq req) {
        DoHomeworkEntity entity = CopyUtil.copy(req, DoHomeworkEntity.class);
        if(ObjectUtils.isEmpty(req.getDid())){
        entity.setDid(snowFlake.nextId());
        doHomeWorkMapper.insert(entity);
    }else {
            System.out.println(entity);
            doHomeWorkMapper.updateById(entity);
        }
    }
}

